<?php 
/**********************************INCLUDE*********************************** *
* **************************************************************************** */
include_once '../app/App.php';
include_once '../account/Account.php';
include_once '../db/DatabaseManager.php';

/**
 * @author nicholas malacarne <nicholas.malacarne@gmail.com>
 * 
 * This script is used to log an account into the app.
 */
// required post data fields -nm
$required = array( 'email', 'password' );

try {
    
    // check if we have required parameters -nm
    if ( ! App::paramOK( $required, $_POST ) ){
        
        throw new ErrorException( "Error Processing Parameters !" );
    }
    
    // get instance of App -nm
    $app = App::init();
    
    // create db manager obj -nm
    $db = new DatabaseManager( 'localhost', 'root', 'admin', 'gtcaitp' );
    
    // strip post data -nm
    $email  = filter_input( INPUT_POST, 'email', FILTER_SANITIZE_EMAIL );
    $pw     = filter_input( INPUT_POST, 'password' );
    
    // TODO: query database -nm
    $data = $db->getRowByValue( 'accounts', 'email', $email );
    
    // TODO: create new account object from database data -nm
    // TODO: set App account -nm
    // TODO: return necessary data or redirect -nm
    
    // create temporary account for testing -nm
    //$app->setAccount( new Account( array( "name" => "Nicholas" ) ) );
    
}catch( Exception $e ){
    
    $report[ 'exception' ] = array();
    
    $report[ 'exception' ][ 'file' ] = $e->getFile();
    $report[ 'exception' ][ 'line' ] = $e->getLine();
    $report[ 'exception' ][ 'msg' ] = $e->getMessage();
    
    echo json_encode( $report );
    
} // TODO: handle specific exception -nm

exit();
